package start.spring.basic.job.controller.jobmonitor;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.security.annotation.RequiresPermissions;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import start.spring.basic.job.common.SysConstant;
import start.spring.basic.job.controller.BaseController;
import start.spring.basic.job.entity.GgPzJob;
import start.spring.basic.job.quartz.TaskBean;
import start.spring.basic.job.quartz.TaskService;
import start.spring.basic.job.service.jobmonitor.IGgPzJobService;
import start.spring.basic.log.ServiceLog;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.response.ResponseData;
import start.spring.basic.response.ResponsePage;

/**
 * 
 * @author xyy
 * @date 2023-06-26
 *
 */
@Slf4j
@RestController
@RequestMapping("/jobmonitor")
@Api(tags = "定时任务相关接口")
public class GgPzJobController extends BaseController {
	
    @Autowired
    private IGgPzJobService ggPzJobService;
    
    @Autowired
    private TaskService taskService;
	
    @ServiceLog(businessType = "查询定时任务列表", systemType = SysConstant.SYSTEM_TYPE, recordResult = false, userId = "")
    @RequiresPermissions("job:jobmonitor:list")
    @PostMapping("/list")
    @ApiOperation(value = "查询定时任务列表", notes = "分页查询")
    public ResponsePage<GgPzJob> list(PageQuery pageQuery, @RequestBody GgPzJob ggPzJob) {
    	return super.returnResponse(this.ggPzJobService.selectGgPzJobPageList(pageQuery, ggPzJob));
    }
    
    @ServiceLog(businessType = "查询定时任务详情", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("job:jobmonitor:list")
    @PostMapping("/detail")
    @ApiOperation(value = "查询定时任务详情", notes = "单条记录详情查询")
    public ResponseData<GgPzJob> detail(@RequestBody GgPzJob ggPzJob) {
    	PageResult<GgPzJob> pageResult = this.ggPzJobService.selectGgPzJobPageList(new PageQuery(), ggPzJob);
    	if(pageResult.getRecords()!=null&&!pageResult.getRecords().isEmpty()) {
    		GgPzJob detail = pageResult.getRecords().get(0);
    		return ResponseData.successData(detail);
    	}else {
    		return ResponseData.errorMsg("未查询到详细信息！");
    	}
    }
	
    @ServiceLog(businessType = "修改定时任务", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("job:jobmonitor:edit")
    @PostMapping("/edit")
    @ApiOperation(value = "修改定时任务", notes = "")
    public ResponseData<Integer> edit(@RequestBody GgPzJob ggPzJob) {
		try {
			TaskBean task = new TaskBean();
	    	task.setJobGroup(ggPzJob.getJobclass());
	    	task.setJobName(ggPzJob.getJobclass());
			boolean jobExist = taskService.isJobExist(task);
			
			if(jobExist) {
				return ResponseData.successData(this.ggPzJobService.updateGgPzJob(ggPzJob));
			}else {
				return ResponseData.errorMsg("任务不存在！");
			}
		} catch (Exception e) {
			log.error("修改定时任务出错！", e);
			return ResponseData.errorMsg("修改定时任务出错！");
		}
    }
    
    @ServiceLog(businessType = "立即执行定时任务", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("job:jobmonitor:list")
    @GetMapping("/runjob")
    @ApiOperation(value = "立即执行定时任务", notes = "")
    public ResponseData<String> runJob(String jobClass) {
    	TaskBean task = new TaskBean();
    	task.setJobClass(jobClass);
    	task.setJobGroup(jobClass);
    	task.setJobName(jobClass);
    	try {
			boolean jobExist = taskService.isJobExist(task);
			if(jobExist) {
				taskService.runJob(task);
				return ResponseData.successMsg("任务已重新执行，请查看执行日志！");
			}else {
				return ResponseData.errorMsg("任务不存在！");
			}
		} catch (SchedulerException e) {
			log.error(jobClass + "立即执行出错！", e);
			return ResponseData.errorMsg("任务执行出错！");
		}
    }
    
    @ServiceLog(businessType = "更新已加载定时任务", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("job:jobmonitor:list")
    @PostMapping("/updateloadedjob")
    @ApiOperation(value = "更新已加载定时任务", notes = "")
    public ResponseData<String> updateLoadedJob(@RequestBody TaskBean task) {
    	try {
			boolean jobExist = taskService.isJobExist(task);
			if(jobExist) {
				taskService.updateJob(task);
				return ResponseData.successMsg("任务已更新！");
			}else {
				return ResponseData.errorMsg("任务不存在！");
			}
		} catch (SchedulerException e) {
			log.error(task.getJobClass() + "更新出错！", e);
			return ResponseData.errorMsg("任务更新出错！");
		}
    }
}
